package rj.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
@WebServlet(name="CountDIsplayServlet",urlPatterns = "/CountDIsplayServlet")
public class CountDisplayServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        // 取出 ServletContext 中存的次数
        Integer count = (Integer) context.getAttribute("count");
        if (count == null) {
            count = 0; // 如果 count 为 null，则初始化为 0（理论上不应该发生，除非 ServletContext 被重置）
        }

        // 显示到页面
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<h1>网站被访问了 " + count + " 次!</h1>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}